var app = getApp();
var Bmob = require('../../utils/bmob.js');
var Bmap = require('../../utils/bmap-wx.js');
Page({
  data: {
    filterId: 1,
    address: '定位中…',
    banners: [],
    icons: [
      [
        {
          id: 1,
          img: '/imgs/index/icon_1.jpg',
          name: '商家入驻',
          url: ''
        },
        {
          id: 2,
          img: '/imgs/index/icon_2.jpg',
          name: '安保服务',
          url: ''
        },
        {
          id: 3,
          img: '/imgs/index/icon_3.jpg',
          name: '快递物流',
          url: ''
        },
        {
          id: 4,
          img: '/imgs/index/icon_4.jpg',
          name: '花店',
          url: ''
        },
        {
          id: 5,
          img: '/imgs/index/icon_5.jpg',
          name: '药店诊所',
          url: ''
        },
        {
          id: 6,
          img: '/imgs/index/icon_6.jpg',
          name: '生鲜果蔬',
          url: ''
        },
        {
          id: 7,
          img: '/imgs/index/icon_7.jpg',
          name: '日用百货',
          url: ''
        },
        {
          id: 8,
          img: '/imgs/index/icon_8.jpg',
          name: '教育培训',
          url: ''
        }
      ],
      [
        {
          id: 9,
          img: '/imgs/index/icon_9.jpg',
          name: '宠物店',
          url: ''
        },
        {
          id: 10,
          img: '/imgs/index/icon_10.jpg',
          name: '汽车之家',
          url: ''
        },
        {
          id: 11,
          img: '/imgs/index/icon_11.jpg',
          name: '家装市场',
          url: ''
        },
        {
          id: 12,
          img: '/imgs/index/icon_12.jpg',
          name: '旅游天地',
          url: ''
        },
        {
          id: 13,
          img: '/imgs/index/icon_13.jpg',
          name: '少儿天地',
          url: ''
        },
        {
          id: 14,
          img: '/imgs/index/icon_14.jpg',
          name: '数码世界',
          url: ''
        },
        {
          id: 15,
          img: '/imgs/index/icon_15.jpg',
          name: '社群营销',
          url: ''
        },
        {
          id: 16,
          img: '/imgs/index/icon_16.jpg',
          name: '微信小程序',
          url: ''
        }
      ]
    ],
    servicelist: [], //服务集市列表
    page: 0,  //分页
    pageLoading: false
  },
  onLoad: function () {
    var self = this;
    var BMap = new Bmap.BMapWX({
      ak: 'wBGctrXaG9Sk7oYuukkE3LFzyLZUaBtk'
    });
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        BMap.regeocoding({
          fail: function (e) {
            console.log(e);
          },
          success: function (res) {
            self.setData({
              address: res.originalData.result.addressComponent.city
            });
          }
        });
      }
    });
  },
  onShow: function () {
    this.fetchServiceData();//加载商家
    this.fetchBannerData();//加载首页banner
  },
  onScroll: function (e) {
    if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }
  },
  tapSearch: function () { //搜索
    wx.navigateTo({ url: 'search' });
  },
  toNearby: function () { // 附近
    var self = this;
    self.setData({
      scrollIntoView: 'nearby'
    });
    setTimeout(function () {
      self.setData({
        scrollIntoView: ''
      });
    });
  },
  tapFilter: function (e) {
    switch (e.target.dataset.id) {
      case '1':
        this.fetchServiceData();
        break;
      case '2':
        this.data.servicelist.sort(function (a, b) {
          return b.createdAt - a.createdAt;
        });
        break;
      case '3':
        this.fetchServiceData("-store_hot");
        break;
    }
    this.setData({
      filterId: e.target.dataset.id,
      servicelist: this.data.servicelist
    });
  },
  tapBanner: function (e) {
    var name = this.data.banners[e.target.dataset.id].name;
    wx.showModal({
      title: '提示',
      content: '您点击了“' + name + '”活动链接，活动页面暂未完成！',
      showCancel: false
    });
  },
  fetchBannerData: function () { // 加载首页滚动图
    var self = this;
    var query = Bmob.Query("Banner");
    query.equalTo("type", '==', "index");
    query.find().then(result => {
      var banners = [];
      for (var i = 0; i < result.length; i++) {
        var bannerObj = {};
        bannerObj.id = result[i].objectId;
        bannerObj.img = result[i].img.url;
        bannerObj.url = result[i].url;
        bannerObj.name = result[i].name;
        banners.push(bannerObj);
      }
      self.setData({
        banners: banners
      });

    }).catch(error => {
      console.log(error);
      wx.hideLoading();
    });
  },
  fetchServiceData: function (orderColumn) { //加载商家列表
    let self = this;
    wx.showLoading({
      title: '加载商家...',
    })
    this.setData({
      pageLoading: true
    });
    var businessQuery = Bmob.Query('Business');
    if (app.isNullEmptyOrUndefined(orderColumn)) {
      businessQuery.order("-createdAt","-store_hot");
    }
    businessQuery.find().then(result => {
      self.setData({
        servicelist: result,
        pageLoading: false
      });
      wx.hideLoading();
    }).catch(error => {
      console.log(error);
      wx.hideLoading();
    });

    // wx.showToast({
    //   title: '加载中',
    //   icon: 'loading'
    // })
    // const perpage = 3;
    // this.setData({
    //   page: this.data.page + 1
    // })
    // const page = this.data.page;
    // const newlist = [];
    // for (var i = (page - 1) * perpage; i < page * perpage; i++) {
    //   newlist.push({
    //     "id": i + 1,
    //     "name": "上海拜特信息技术有限公司" + (i + 1),
    //     "city": "上海",
    //     "tag": "法律咨询",
    //     "imgurl": "http://img.mukewang.com/57fdecf80001fb0406000338-240-135.jpg"
    //   })
    // }
    // setTimeout(() => {
    //   _this.setData({
    //     servicelist: _this.data.servicelist.concat(newlist)
    //   })
    // }, 1500)
  }
});

